<html>
<head><meta charset="utf-8"><title>Error downcast design · project-error-handling · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/index.html">project-error-handling</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html">Error downcast design</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232179332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jam1garner <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179332">(Mar 28 2021 at 17:06)</a>:</h4>
<p>hopefully quick question to clear up some confusion for me: is there a specific reason Error::downcast is implemented by casting Box&lt;Error&gt; -&gt; raw ptr -&gt; Box&lt;T&gt;? I was implementing a similar error type for a crate and we were able to achieve the same thing using a blanket implementation for upcasting to Box&lt;Any&gt; then downcasting + unwrapping after the Any::is typecheck. curious because I'm worried I'm missing some downside that std::error::Error wanted to avoid</p>



<a name="232179392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179392">(Mar 28 2021 at 17:07)</a>:</h4>
<p>I think it's because the downcast functionality on Error is a reimplementation of Any</p>



<a name="232179393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jam1garner <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179393">(Mar 28 2021 at 17:07)</a>:</h4>
<p>for reference, this is effectively the change I'm describing: <a href="https://github.com/jam1garner/binrw/commit/e055ab6e73401e435c331cbe46173ddc73ab5390">https://github.com/jam1garner/binrw/commit/e055ab6e73401e435c331cbe46173ddc73ab5390</a><br>
and this is what such a blanket impl would look like: <a href="https://github.com/jam1garner/binrw/blob/e055ab6e73401e435c331cbe46173ddc73ab5390/binrw/src/error.rs#L19-L42">https://github.com/jam1garner/binrw/blob/e055ab6e73401e435c331cbe46173ddc73ab5390/binrw/src/error.rs#L19-L42</a></p>



<a name="232179409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179409">(Mar 28 2021 at 17:08)</a>:</h4>
<p>So presumably the downcast on Any is doing the same thing Error does</p>



<a name="232179453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179453">(Mar 28 2021 at 17:08)</a>:</h4>
<p>I'm guessing it was just copied</p>



<a name="232179519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jam1garner <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179519">(Mar 28 2021 at 17:09)</a>:</h4>
<p>I see. Would a implementation like the above be desirable to keep Error in sync with Any? not that I expect much changing for either implementation...</p>



<a name="232179584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179584">(Mar 28 2021 at 17:10)</a>:</h4>
<p>I doubt it, I imagine that going through Any would require extra indirection and vtables</p>



<a name="232179597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179597">(Mar 28 2021 at 17:10)</a>:</h4>
<p>Not sure on that though</p>



<a name="232179607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179607">(Mar 28 2021 at 17:10)</a>:</h4>
<p>This is a bit lower level than I tend to work at</p>



<a name="232179621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179621">(Mar 28 2021 at 17:10)</a>:</h4>
<p>Also worth noting, with generic member access your AsAny stuff will essentially be part of the Error trait</p>



<a name="232179629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179629">(Mar 28 2021 at 17:11)</a>:</h4>
<p>Because generic member access supports arbitrary up casting</p>



<a name="232179654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179654">(Mar 28 2021 at 17:11)</a>:</h4>
<p>I really need to finish that RFC and get it implemented in std</p>



<a name="232179657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179657">(Mar 28 2021 at 17:11)</a>:</h4>
<p>Bah</p>



<a name="232179721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257204-project-error-handling/topic/Error%20downcast%20design/near/232179721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jam1garner <a href="https://rust-lang.github.io/zulip_archive/stream/257204-project-error-handling/topic/Error.20downcast.20design.html#232179721">(Mar 28 2021 at 17:12)</a>:</h4>
<p>heh. yeah I suppose most of this is just a fancy upcasting hack. thanks, good to know :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>